﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using YJKRevitDictionary.Operation;

namespace YJKRevitDictionary.Forms
{
	public partial class DlgEditDirectory : System.Windows.Forms.Form
	{
		public DlgEditDirectory(DlgDict mForm)
		{
			this.mainForm = mForm;
			this.InitializeComponent();
			if (this.mainForm.tvDirectory.SelectedNode != null)
			{
				this.tbxName.Text = this.mainForm.tvDirectory.SelectedNode.Text;
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.mainForm.tvDirectory.SelectedNode != null)
			{
				if (DictOperation.EditDirectory((XmlNode)this.mainForm.tvDirectory.SelectedNode.Tag, this.tbxName.Text, this.mainForm.dictFile.FullName))
				{
					DictOperation dictOperation = new DictOperation(this.mainForm.dictFile.FullName);
					dictOperation.FillCbx(this.mainForm.comboBox1);
					dictOperation.FillTree(this.mainForm.tvDirectory);
					this.mainForm.lbxEntry.Items.Clear();
					XmlNode node = (XmlNode)this.mainForm.tvDirectory.Nodes[0].Tag;
					this.mainForm.GetChildAttributes(node, this.mainForm.lbxEntry);
					this.mainForm.lbxEntry.Sorted = true;
					this.mainForm.richTextBox1.Text = "";
					base.DialogResult = DialogResult.OK;
				}
				else
				{
					base.DialogResult = DialogResult.Cancel;
				}
			}
			base.Close();
		}

		public static DlgEditDirectory GetInstance(DlgDict main)
		{
			if (DlgEditDirectory.ded == null || DlgEditDirectory.ded.IsDisposed)
			{
				DlgEditDirectory.ded = new DlgEditDirectory(main);
				DlgEditDirectory.ded.MdiParent = main.ActiveMdiChild;
			}
			else
			{
				DlgEditDirectory.ded.mainForm = main;
			}
			return DlgEditDirectory.ded;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void tbxName_TextChanged(object sender, EventArgs e)
		{
			if (this.tbxName.Text.Trim() != "")
			{
				this.btnOK.Enabled = true;
				return;
			}
			this.btnOK.Enabled = false;
		}

		private DlgDict mainForm;

		private static DlgEditDirectory ded;
	}
}
